package asteroids.model.expressions;

import exceptions.IllegalOperandException;

/**
 * A subclass of expression (E) that represents the comparison (==) of to objects
 * 
 * @version 1.0
 * 
 * @author	Thijs Peirelinck & Wouter Parys
 *
 */
public class Equals extends Comparison {
	
	public Equals(E e1, E e2) {
		super(e1, e2);
	}
	
	@Override
	public boolean isValidExpression() {
		if(getLeftOperand().getReturnType().equals("boolean") && getRightOperand().getReturnType().equals("boolean") && !(getLeftOperand() == this) && !(getRightOperand() == this)) {
			return true;
		}
		if(getLeftOperand().getReturnType().equals("double") && getRightOperand().getReturnType().equals("double") && !(getLeftOperand() == this) && !(getRightOperand() == this)) {
			return true;
		}
		if(getLeftOperand().getReturnType().equals("entity") && getRightOperand().getReturnType().equals("entity") && !(getLeftOperand() == this) && !(getRightOperand() == this)) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean getValue() {
		if(!this.isValidExpression()) { throw new IllegalOperandException(); }
		if (getLeftOperand().getReturnType().equals("double")) {
			return asteroids.Util.fuzzyEquals(((Double)getLeftOperand().getValue()), ((Double)getRightOperand().getValue()));
		}
		else 
			return (getLeftOperand().getValue() == getRightOperand().getValue());
	}
	
	@Override
	public String toString() {
		return getLeftOperand().toString() + " == " + getRightOperand().toString();
	}

	@Override
	public Equals createCopy() {
		return new Equals(getLeftOperand().createCopy(), getRightOperand().createCopy());
	}

}
